package org.leiyang.entries;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * 临时文件遍历器
 */
@Data
@AllArgsConstructor
public class TempFileIterator implements Comparable<TempFileIterator> {

    private ObjectInputStream ois;

    private Record current;

    public TempFileIterator(ObjectInputStream ois) throws IOException {
        this.ois = ois;
        readNext();
    }

    public Boolean readNext() throws IOException {
        try {
            int id = ois.readInt();
            int count = ois.readInt();
            float price = ois.readFloat();
            current = new Record(id, count, price);
            return true;
        } catch (EOFException e) {
            return false;
        }
    }

    @Override
    public int compareTo(TempFileIterator o) {
        return this.current.compareTo(o.current);
    }
}
